ForEach(TSource, TLocal) Method (OrderablePartitioner(TSource), ParallelOptions, Func(TLocal), Func(TSource, ParallelLoopState, Int64, TLocal, TLocal), Action(TLocal))

Task Parallel System.Threading

Executes a for each operation on a OrderablePartitioner in which iterations may run in parallel.

Namespace:  System.Threading.Tasks
Assembly:  System.Threading (in System.Threading.dll)

Syntax

Visual Basic (Declaration)
Public Shared Function ForEach(Of TSource, TLocal) ( _
	source As OrderablePartitioner(Of TSource), _
	parallelOptions As ParallelOptions, _
	localInit As Func(Of TLocal), _
	body As Func(Of TSource, ParallelLoopState, Long, TLocal, TLocal), _
	localFinally As Action(Of TLocal) _
) As ParallelLoopResult
C#
public static ParallelLoopResult ForEach<TSource, TLocal>(
	OrderablePartitioner<TSource> source,
	ParallelOptions parallelOptions,
	Func<TLocal> localInit,
	Func<TSource, ParallelLoopState, long, TLocal, TLocal> body,
	Action<TLocal> localFinally
)

Parameters

source
Type: System.Collections.Concurrent..::.OrderablePartitioner<(Of <(TSource>)>)
The OrderablePartitioner that contains the original data source.
parallelOptions
Type: System.Threading.Tasks..::.ParallelOptions
A ParallelOptions instance that configures the behavior of this operation.
localInit
Type: System..::.Func<(Of <(TLocal>)>)
The function delegate that returns the initial state of the local data for each thread.
body
Type: System..::.Func<(Of <(TSource, ParallelLoopState, Int64, TLocal, TLocal>)>)
The delegate that is invoked once per iteration.
localFinally
Type: System..::.Action<(Of <(TLocal>)>)
The delegate that performs a final action on the local state of each thread.

Type Parameters

TSource
The type of the elements in source.
TLocal
The type of the thread-local data.

Return Value

A ParallelLoopResult structure that contains information on what portion of the loop completed.

Remarks

The Partitioner is used to retrieve the elements to be processed, in place of the original data source. If the current element's index is desired, the source must be an OrderablePartitioner.

The body delegate is invoked once for each element in the source Partitioner. It is provided with the following parameters: the current element, a ParallelLoopState instance that may be used to break out of the loop prematurely, the current element's index (an Int64), and some local state that may be shared amongst iterations that execute on the same thread.

The localInit delegate is invoked once for each thread that participates in the loop's execution and returns the initial local state for each of those threads. These initial states are passed to the first body invocations on each thread. Then, every subsequent body invocation returns a possibly modified state value that is passed to the next body invocation. Finally, the last body invocation on each thread returns a state value that is passed to the localFinally delegate. The localFinally delegate is invoked once per thread to perform a final action on each thread's local state.

Exceptions

ExceptionCondition
System..::.ArgumentNullExceptionThe exception that is thrown when the source argument is null.
System..::.ArgumentNullExceptionThe exception that is thrown when the parallelOptions argument is null.
System..::.ArgumentNullExceptionThe exception that is thrown when the body argument is null.
System..::.ArgumentNullExceptionThe exception that is thrown when the localInit argument is null.
System..::.ArgumentNullExceptionThe exception that is thrown when the localFinally argument is null.
System..::.OperationCanceledExceptionThe exception that is thrown when the CancellationToken in the parallelOptions argument is set
System..::.InvalidOperationExceptionThe exception that is thrown when the SupportsDynamicPartitions property in the source OrderablePartitioner returns false.
System..::.InvalidOperationExceptionThe exception that is thrown when the KeysNormalized property in the source OrderablePartitioner returns false.
System..::.InvalidOperationExceptionThe exception that is thrown when any methods in the source OrderablePartitioner return null.
System..::.InvalidOperationExceptionThe exception that is thrown when the GetPartitions() or GetOrderablePartitions() methods in the source OrderablePartitioner do not return the correct number of partitions.
System..::.InvalidOperationExceptionThe exception that is thrown when the GetPartitions() or GetOrderablePartitions() methods in the source OrderablePartitioner return an IList with at least one null value.
System..::.InvalidOperationExceptionThe exception that is thrown when the GetDynamicPartitions() or GetDynamicOrderablePartitions() methods in the source OrderablePartitioner return an IEnumerable whose GetEnumerator() method returns null.
System..::.AggregateExceptionThe exception that is thrown to contain an exception thrown from one of the specified delegates.
System..::.ObjectDisposedExceptionThe exception that is thrown when the the CancellationTokenSource associated with the the CancellationToken in the parallelOptions has been disposed.

See Also